import getStartEndDate from '../../core/util/getStartEndDate';

function inspectContentMonthCtrl($rootScope, $scope, $http, ngDialog, paFetchService, ngDialogProblem) {
  'ngInject';

  // 判断首页进来的type
  let type = $state.params.type;
  let bidId = $state.params.bidId;
  let orgId = $state.params.orgId;

  // 查询条件的对象
  this.queryData = {
    queryType: 0, // pc:0,app:1
  };

  // 本月
  var myDate = new Date();
  this.years = myDate.getFullYear();
  this.month = myDate.getMonth();

  let startEndDate = getStartEndDate(this.years, this.month);

  this.queryData.statusList = [];
  this.queryData.suspendingDisabled = false; // 待处理隐患 选择查询时间

  if (type == 'month') {
    // 本月
    this.queryData.carryOutOrgId = orgId;
    this.queryData.problemFoundTimeFrom = startEndDate.startDateStr; // 巡查开始时间
    this.queryData.problemFoundTimeTo = startEndDate.endDateStr;    
  } else if (type == 'problem') {
    // 本月不符合要求
    this.queryData.carryOutOrgId = orgId;
    this.queryData.checkResultDisabled = false; // 巡查结果不可点击
    this.queryData.problemFoundTimeFrom = startEndDate.startDateStr; // 巡查开始时间
    this.queryData.problemFoundTimeTo = startEndDate.endDateStr;
    this.queryData.statusList.push(0,1,2);
  } else if (type == 'suspending') {
    if (bidId) { // 标
      this.queryData.handleOrgId = orgId;
      this.queryData.statusList.push(0,1);
    } else { // 总监办
      this.queryData.orgId = orgId;
      this.queryData.statusList.push(1);
    }       
    this.queryData.checkResultDisabled = false; // 巡查结果可点击
    this.queryData.suspendingDisabled = true;
  }

  // 分页
  this.paginationData = {
    pageNum: 0,
    pageSize: 10,
    total: '',
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {
  	this.paginationData1 = {
  	  pageNum: this.paginationData.pageNum-1,
  	  pageSize: 10,
  	  total: '',
  	}

    $http.post(`${SERVER_CONTEXT}/api/checkRecord/app/queryCheckProblemListByCondition`,
      angular.extend(this.queryData, this.paginationData1)
        )
      .then((resp) => {
        this.inspectContentMonthList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 分页查询方法
  this.paginationQuery = () => {
    if (this.paginationData.pageNum) {
      this.paginationData2 = {
        pageNum: this.paginationData.pageNum-1,
        pageSize: 10,
        total: '',
      }
    } else {
      this.paginationData2 = {
        pageNum: 0,
        pageSize: 10,
        total: '',
      }
    }
    $http.post(`${SERVER_CONTEXT}/api/checkRecord/app/queryCheckProblemListByCondition`,
      angular.extend(this.queryData, this.paginationData2)
        )
      .then((resp) => {
        this.inspectContentMonthList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 重置
  this.reset = () => {
    this.queryData = {
    	queryType: 0,
      statusList: [],
    };
    if (type == 'month') {
      this.queryData.carryOutOrgId = orgId;
      this.queryData.problemFoundTimeFrom = startEndDate.startDateStr; // 巡查开始时间
      this.queryData.problemFoundTimeTo = startEndDate.endDateStr;
    } else if (type == 'problem') {
      this.queryData.carryOutOrgId = orgId;
      this.queryData.problemFoundTimeFrom = startEndDate.startDateStr; // 巡查开始时间
      this.queryData.problemFoundTimeTo = startEndDate.endDateStr;
      this.queryData.statusList.push(0,1,2);
    } else if (type == 'suspending') {
      // 待处理隐患
      if (bidId) { // 标
        this.queryData.handleOrgId = orgId;
        this.queryData.statusList.push(0,1);
      } else { // 总监办
        this.queryData.orgId = orgId;
        this.queryData.statusList.push(1);
      } 
    }
  }

  // 查询
  this.query = (param) => {
    if (param.endTime.$viewValue && param.startTime.$viewValue) {
      if (param.startTime.$viewValue < param.endTime.$viewValue) {
        this.paginationQuery();
    } else {
        layer.alert('巡查提交开始时间应小于结束时间');
      }
    } else {
      this.paginationQuery();
    }
  }

  // 查看内容详情
  this.checkDetail = (id) => {
    ngDialogProblem
      .confirm(id)
      .then((resolve) => {
        this.paginationQuery();
      });
  }

  // 删除巡查内容列表
  this.deleteInspectContent = (id) => {
    $scope.modalTitle = '删除巡查内容';
    $scope.modalContent = '确认要删除该信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
      then((value) => {
        $http.delete(`${SERVER_CONTEXT}/api/securityActivities/checkProblem/deleteCheckProblem/${id}`).
          then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);
              $state.reload();
            } else {
              layer.alert(resp.data.message);
            }
          });
      }, (reason) => {});
  }

  this.initPage = () => {
    this.paginationQuery();
  }

  this.initPage();


}

export default inspectContentMonthCtrl;